<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  iphone
 */

class m_iphone_actions_IphoneAddFolderAction extends f_core_Action
{
	public function execute()
	{
        $docId = $this->getRequestParameter("docId");
        $docLang = $this->getRequestParameter("docLang", $this->getContext()->getDefaultLang());
        $folderType = $this->getRequestParameter("folderType", $this->getRequestParameter("configModule"));
        $docLabel = $this->getRequestParameter("docLabel");

        if($docId == "0" && !is_null($folderType))
        {
            $rootNode = m_backoffice_services_RootNodeService::getInstance()->getModuleRootNode($folderType, $docLang);
            $docId = is_null($rootNode) ? null : $rootNode->getUniqueId();
        }

        if(!is_null($docId) && !is_null($folderType) && $this->getDocumentProvider()->checkDocument($docId, $docLang))
		{
            if(isset($docLabel) && !empty($docLabel))
            {
                $parent = $this->getDocumentProvider()->getByUniqueId($docId, $docLang);
                $folder = m_backoffice_documents_Folder::createNew()->setLang($docLang)->setModule($folderType);

                $folder->setLabel($docLabel)->save(false);
				$parent->addFolder($folder)->save(false);
            }
            else
            {
                $error = true;
                $msg = $this->getLocale('backoffice.errors.required');
            }
		}
        else
        {
            $error = true;
        }

        $xml = m_iphone_services_IphoneService::getInstance()->genericConfirm($error, $msg);
        return $xml;
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight("backoffice_accessBo");
	}
	
	public function errorHandler()
	{
		$this->setView('error');
	}
}